package com.gitee.feizns.explore.data_structure.tree.bs.easy;

/**
 * @author feizns
 * @since 2019/12/25
 */
public class MinDepth {

    public static void main(String[] args) {
        System.out.println(minDepth(create()));
    }

    private static TreeNode create() {
        TreeNode ret = new TreeNode(1);
        ret.left = new TreeNode(2);
        ret.right = new TreeNode(2);
        ret.left.left = new TreeNode(3);
        ret.left.left.left = new TreeNode(4);
        return ret;
    }

    private static int minDepth(TreeNode root) {
        if ( root == null ) return 0;
        int left = minDepth(root.left);
        int right = minDepth(root.right);
        if ( left == 0 || right == 0 )
            return left + right + 1;
        return Math.min(left, right) + 1;
    }

}
